import React, {Component} from 'react'
import BackPressComponent from '../common/BackPressComponent'
import {BackHandler} from 'react-native'
import {
    View,
    Text,
    StyleSheet,
} from 'react-native'
import navigatorUtil from '../navigator/navigatorUtil'
import DynamicTabNavigator from '../navigator/DynamicTabNavigator'
import {connect} from 'react-redux'
import {NavigationActions} from 'react-navigation'

class HomePage extends Component {
    constructor(props) {
        super(props)
        // this.backPress = new BackPressComponent({backPress: this.onBackPress()})
    }

    componentDidMount() {
        // this.backPress.componentDidMount()
        // const {}
        BackHandler.addEventListener('hardwareBackPress',this.onBackPress)
    }

    componentWillUnmount() {
        // this.backPress.componentWillUnmount()
        BackHandler.removeEventListener('hardwareBackPress',this.onBackPress)
    }

    onBackPress = () => {
        const {nav, dispatch} = this.props
        if (nav.routes[1].index === 0) {
            return false
        }
        dispatch(NavigationActions.back())
        return true

    }

    render() {
        navigatorUtil.navigation = this.props.navigation
        return <DynamicTabNavigator/>
    }
}

const styles = StyleSheet.create({
    welcome: {
        flex: 1,
        alignItems: 'center',
        justifyContent: 'center',
    }
})
const mapState = state => ({
    nav: state.nav
})
export default connect(mapState, null)(HomePage)
